package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.activityrecord;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.activity.ActivityConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.activity.ActivityDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.activity.ActivityService;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.activityrecord.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.activityrecord.ActivityRecordDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.activityrecord.ActivityRecordConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.activityrecord.ActivityRecordService;

@Tag(name = "管理后台 - 活动参加记录")
@RestController
@RequestMapping("/fengdong.smartelderly/activity-record")
@Validated
public class ActivityRecordController {

    @Resource
    private ActivityRecordService activityRecordService;

    @Resource
    private ActivityService activityService;

    @Resource
    private MemberUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建活动参加记录")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record:create')")
    public CommonResult<Long> createActivityRecord(@Valid @RequestBody ActivityRecordCreateReqVO createReqVO) {
        return success(activityRecordService.createActivityRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动参加记录")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record:update')")
    public CommonResult<Boolean> updateActivityRecord(@Valid @RequestBody ActivityRecordUpdateReqVO updateReqVO) {
        activityRecordService.updateActivityRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动参加记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record:delete')")
    public CommonResult<Boolean> deleteActivityRecord(@RequestParam("id") Long id) {
        activityRecordService.deleteActivityRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动参加记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record:query')")
    public CommonResult<ActivityRecordRespVO> getActivityRecord(@RequestParam("id") Long id) {
        ActivityRecordDO activityRecord = activityRecordService.getActivityRecord(id);
        return success(ActivityRecordConvert.INSTANCE.convert(activityRecord));
    }

//    @GetMapping("/list")
//    @Operation(summary = "获得活动参加记录列表")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record:query')")
//    public CommonResult<List<ActivityRecordRespVO>> getActivityRecordList(@RequestParam("ids") Collection<Long> ids) {
//        List<ActivityRecordDO> list = activityRecordService.getActivityRecordList(ids);
//        return success(ActivityRecordConvert.INSTANCE.convertList(list));
//    }

    @GetMapping("/unitepage")
    @Operation(summary = "获得活动参加记录分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record-unite:query')")
    public CommonResult<PageResult<ActivityRecordUniteRespVO>> getActivityRecordUnitePage(@Valid ActivityRecordUnitePageReqVO pageVO) {
        return success(activityRecordService.getActivityRecordUnitePage(pageVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动参加记录分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record:query')")
    public CommonResult<PageResult<ActivityRecordRespVO>> getActivityRecordPage(@Valid ActivityRecordPageReqVO pageVO) {
        PageResult<ActivityRecordDO> pageResult = activityRecordService.getActivityRecordPage(pageVO);
        PageResult<ActivityRecordRespVO> activityRecordRespVOPageResult = ActivityRecordConvert.INSTANCE.convertPage(pageResult);
        activityRecordRespVOPageResult.getList().forEach(activityRecordRespVO -> {
            activityRecordRespVO.setActivityRespVO(ActivityConvert.INSTANCE.convert(activityService.getActivityRespVO(activityRecordRespVO.getActivityId())));
            MemberUserDO user = userService.getUser(Long.valueOf(activityRecordRespVO.getCreator()));
            if (user!=null) {
                activityRecordRespVO.setName(user.getName());
                activityRecordRespVO.setNickname(user.getNickname());
            }
        });
        return success(activityRecordRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动参加记录 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:activity-record:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityRecordExcel(@Valid ActivityRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ActivityRecordDO> list = activityRecordService.getActivityRecordList(exportReqVO);
        // 导出 Excel
        List<ActivityRecordExcelVO> datas = ActivityRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "活动参加记录.xls", "数据", ActivityRecordExcelVO.class, datas);
    }

}
